package cn.edu.zju.om.preprocess;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.zju.om.data.Comment;
import cn.edu.zju.om.data.DAO;
import cn.edu.zju.om.nlp.SUWordSegmentorAdapter;
import cn.edu.zju.om.nlp.WordSegmentor;

public class Preprocessor {
	
	private static WordSegmentor segmentor = new SUWordSegmentorAdapter();
	
	private static final int PAGE_LIMIT = 5000;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DAO<Comment, String> commentDAO = new DAO<Comment, String>(Comment.class);
		//List<Comment> comments = commentDAO.findAll();
		Session session = commentDAO.getSession();
		Query countQuery = session.createQuery("select count(*) from comment");
		int count = ((Integer) countQuery.iterate().next()).intValue();
		System.out.println("Records count = " + count);
		for(int i = 0; i < count; i += PAGE_LIMIT) {
			Query query = session.createQuery("from comment");
			query.setFirstResult(i);
			query.setMaxResults(PAGE_LIMIT);
			
			List<Comment> comments = query.list();
			for(Comment comment : comments) {
				String reviewText = comment.getText();
				try {
					List<String> seggedTextList = segmentor.segment(reviewText);
					StringBuffer sb = new StringBuffer();
					for(String word : seggedTextList) {
						sb.append(word);
						sb.append(' ');
					}
					comment.setSeggedText(sb.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			commentDAO.makePersistent(comment);	
			}
		}

	}

}
